package com.lh.pospi.x_store.view;

import android.content.Context;
import android.content.DialogInterface;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lh.pospi.x_store.R;
import com.lh.pospi.x_store.modle.GoodsBean;
import com.vondear.rxtool.RxKeyboardTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.dialog.RxDialog;
import com.vondear.rxui.view.dialog.RxDialogEditSureCancel;

public class DialogGoods {
    public interface Listener{
        void getNum(int num);
    }
    private Context mContext;
    private RxDialog dialog;

    public DialogGoods(Context mContext) {
        this.mContext = mContext;
    }

    public void show(GoodsBean bean , final Listener listener) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_goods, null);
        TextView d_name = view.findViewById(R.id.d_name);
        TextView d_code = view.findViewById(R.id.d_code);
        TextView d_price = view.findViewById(R.id.d_price);
        TextView d_unit = view.findViewById(R.id.d_unit);
        TextView d_spec = view.findViewById(R.id.d_spec);
        TextView d_cancel = view.findViewById(R.id.d_cancel);
        TextView d_sure = view.findViewById(R.id.d_sure);
        final EditText et_num = view.findViewById(R.id.d_num);
        d_name.setText("名称：" + bean.getName());
        d_code.setText("条码：" + bean.getBarcode());
        d_price.setText("单价：" + bean.getLsj());
        d_unit.setText("单位：" + bean.getUnit());
        d_spec.setText("规格：" + bean.getSpec());
        dialog = new RxDialog(mContext);
        dialog.setContentView(view);
        et_num.setSelectAllOnFocus(true);
        d_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        d_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String n = et_num.getText().toString().trim();
                if (n.isEmpty()) {
                    RxToast.warning("请输入数量");
                } else {
                    listener.getNum(Integer.parseInt(n));
                    dialog.dismiss();
                }
            }
        });
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                RxKeyboardTool.toggleSoftInput(mContext,et_num);
            }
        });
        dialog.setCancelable(false);
        dialog.show();
        RxKeyboardTool.toggleSoftInput(mContext,et_num);
    }

    public void showDeNum(String num, final Listener listener) {
        final RxDialogEditSureCancel sureCancel = new RxDialogEditSureCancel(mContext);
        sureCancel.setTitle("默认数量");
        sureCancel.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
        sureCancel.getEditText().setText(num);
        sureCancel.getEditText().setSelectAllOnFocus(true);
        sureCancel.getSureView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String n = sureCancel.getEditText().getText().toString().trim();
                if (n.isEmpty()) {
                    RxToast.warning("请输入数量");
                } else {
                    closeKey(sureCancel.getEditText());
                    sureCancel.dismiss();
                    listener.getNum(Integer.parseInt(n));
                }

            }
        });
        sureCancel.getCancelView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeKey(sureCancel.getEditText());
                sureCancel.dismiss();
            }
        });
        sureCancel.show();
        RxKeyboardTool.toggleSoftInput(mContext,sureCancel.getEditText());
    }
    private void closeKey(EditText editText){
        RxKeyboardTool.hideSoftInput(mContext, editText);

    }
}
